package cn.jksoft.businessclient.ui.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import cn.jksoft.businessclient.R;
import cn.jksoft.businessclient.bean.OrderDetailEntity;
import cn.jksoft.businessclient.bean.OrderDetailStatusEntity;

public class OrderDetailAdapter extends BaseExpandableListAdapter {

    private static final int TYPE_ITEM_PRODUCT = 0;
    private static final int TYPE_ITEM_STATUS= 1;
    private static final int TYPE_MAX_COUNT = 2;
    private Context context;
    List<String> groups;
    List<Object> childs;
    private String isFastBillingOrder;
    ItemProductViewHolder productViewHolder;
    ItemStatusViewHolder statusViewHolder;

    public class ItemProductViewHolder {
        @Bind(R.id.tv_product_name)
        public TextView tvProductName;
        @Bind(R.id.tv_product_price)
        public TextView tvProductPrice;
        @Bind(R.id.ll_total)
        public RelativeLayout llTotal;
        @Bind(R.id.tv_total_price)
        public TextView tvTotalPrice;
        @Bind(R.id.tv_storage_name)
        public TextView tvStorageName;
        @Bind(R.id.tv_count)
        public TextView tvCount;

        public ItemProductViewHolder(View itemView){
            ButterKnife.bind(this,itemView);
        }
    }

    public class  ItemStatusViewHolder {
        @Bind(R.id.btn_avatar)
        public Button btnAvatar;;
        @Bind(R.id.tv_title)
        public TextView tvTitle;
        @Bind(R.id.tv_desc)
        public TextView tvDesc;
        @Bind(R.id.tv_time)
        public TextView tvTime;

        public ItemStatusViewHolder(View itemView){
            ButterKnife.bind(this,itemView);
        }

    }
    public OrderDetailAdapter(Context context,
                              List<String> groups,
                              List<Object> childs) {
        this.groups = groups;
        this.childs = childs;
        this.context = context;
    }

    public void addChildData(Object child,String isFastOrder){
        this.childs.add(child);
        this.isFastBillingOrder = isFastOrder;
        notifyDataSetChanged();
    }

    public void setChildData(List<Object> childs){
        this.childs = childs;
        notifyDataSetChanged();
    }


    @Override
    public Object getChild(int groupPosition, int childPosition) {
        if(groupPosition == 0){
            OrderDetailEntity.OrderDetailsBean bean = (OrderDetailEntity.OrderDetailsBean)childs.get(groupPosition);
            return bean.getOrderItemsList().get(childPosition);
        }else{
            List<OrderDetailStatusEntity.CustomerReceiptsMessListBean> receiptsMessListBeen = (List<OrderDetailStatusEntity.CustomerReceiptsMessListBean>)childs.get(groupPosition);
            return receiptsMessListBeen.get(childPosition);
        }
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    // 获取二级列表的View对象
    @Override
    public View getChildView(final int groupPosition, final int childPosition,
                             boolean isLastChild, View convertView, ViewGroup parent) {
        int itemType = getChildType(groupPosition,childPosition);
        if(itemType == TYPE_ITEM_PRODUCT){
            ItemProductViewHolder productViewHolder;
            if (convertView == null) {
                LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                // 获取二级列表对应的布局文件, 并将其各元素设置相应的属性
                convertView = layoutInflater.inflate(R.layout.item_order_detail_product, null);
                productViewHolder = new ItemProductViewHolder(convertView);
                this.productViewHolder = productViewHolder;
                convertView.setTag(productViewHolder);
            } else {
                productViewHolder = (ItemProductViewHolder) convertView.getTag();
            }
            OrderDetailEntity.OrderDetailsBean bean = (OrderDetailEntity.OrderDetailsBean)childs.get(groupPosition);
            OrderDetailEntity.OrderDetailsBean.OrderItemsListBean orderItemsListBean= bean.getOrderItemsList().get(childPosition);
            String storageName = orderItemsListBean.getSTORAGE_NAME()!=null?orderItemsListBean.getSTORAGE_NAME():"";
            productViewHolder.tvProductName.setText(orderItemsListBean.getPROD_NAME());
            productViewHolder.tvStorageName.setText(storageName);
            productViewHolder.tvCount.setText(orderItemsListBean.getAMOUNT()+"");
            if("1".equals(isFastBillingOrder)) {
                productViewHolder.tvProductPrice.setText("￥" + orderItemsListBean.getTOTAL_MONEY() / 100);
            }else {
                productViewHolder.tvProductPrice.setText("￥" + orderItemsListBean.getSALE_PRICE() / 100);
            }

            if(childPosition == bean.getOrderItemsList().size()-1){
                productViewHolder.tvTotalPrice.setText("￥"+bean.getORDER_TOTAL_MONEY()/100);
                productViewHolder.llTotal.setVisibility(View.VISIBLE);
            }else{
                productViewHolder.llTotal.setVisibility(View.GONE);
            }

        }else if(itemType == TYPE_ITEM_STATUS){
            final ItemStatusViewHolder statusViewHolder;
            if (convertView == null) {
                LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                // 获取二级列表对应的布局文件, 并将其各元素设置相应的属性
                convertView = layoutInflater.inflate(R.layout.item_order_detail_status, null);
                statusViewHolder = new ItemStatusViewHolder(convertView);
                this.statusViewHolder = statusViewHolder;
                convertView.setTag(statusViewHolder);
            } else {
                statusViewHolder = (ItemStatusViewHolder) convertView.getTag();
            }
            List<OrderDetailStatusEntity.CustomerReceiptsMessListBean> receiptsMessListBeen = (List<OrderDetailStatusEntity.CustomerReceiptsMessListBean>)childs.get(groupPosition);
            OrderDetailStatusEntity.CustomerReceiptsMessListBean bean = receiptsMessListBeen.get(childPosition);
            statusViewHolder.btnAvatar.setText(bean.getNAME().substring(0,1));
            statusViewHolder.tvTitle.setText(bean.getMESS_TITLE());
            statusViewHolder.tvDesc.setText(bean.getMESS_CONTENT());
            statusViewHolder.tvTime.setText(bean.getTIME());
        }
        return convertView;
    }


    @Override
    public int getChildrenCount(int groupPosition) {
        if(childs.size()>0) {
            if (groupPosition == 0) {
                OrderDetailEntity.OrderDetailsBean bean = (OrderDetailEntity.OrderDetailsBean) childs.get(groupPosition);
                if (bean.getOrderItemsList() != null) {
                    return bean.getOrderItemsList().size();
                }
            } else {
                if(childs.size()>1 && childs.get(groupPosition)!=null) {
                    List<OrderDetailStatusEntity.CustomerReceiptsMessListBean> receiptsMessListBeen = (List<OrderDetailStatusEntity.CustomerReceiptsMessListBean>) childs.get(groupPosition);
                    if (receiptsMessListBeen != null) {
                        return receiptsMessListBeen.size();
                    }
                }
            }
        }
        return 0;

    }

    @Override
    public Object getGroup(int groupPosition) {
        return groups.get(groupPosition);
    }

    @Override
    public int getGroupCount() {
        return groups.size();
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    // 获取一级列表View对象
    @Override
    public View getGroupView(int groupPosition, boolean isExpanded,
                             View convertView, ViewGroup parent) {
        if (convertView == null) {
            LayoutInflater layoutInflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            // 获取一级列表布局文件,设置相应元素属性
            convertView = layoutInflater.inflate(R.layout.item_order_detail_group, null);
        }
        TextView tvTitle = (TextView) convertView.findViewById(R.id.item_tv_title);
        tvTitle.setText(groups.get(groupPosition));
        return convertView;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }


    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }


    @Override
    public int getChildType(int groupPosition, int childPosition) {
        if(groupPosition == 0){
            return TYPE_ITEM_PRODUCT;
        }else{
            return TYPE_ITEM_STATUS;
        }
    }

    @Override
    public int getChildTypeCount() {
        return TYPE_MAX_COUNT;
    }
}
